home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 10 / 010.d81 / dos #24 < prev    next >
Text File  |  2022-08-26  |  3KB  |  173 lines

  1.  
  2.       DOS & Don'ts  -- Part  24
  3.            by Jimmy Weiler
  4.  
  5.  
  6.   Last month we discussed the OPENing
  7.  
  8. and CLOSEing of a RELative file.  This
  9.  
  10. month we will leap right into WRITING
  11.  
  12. and READING.
  13.  
  14.   As with any other type of file, you
  15.  
  16. write to a RELative file with a PRINT#
  17.  
  18. statement.  RELative files are unique
  19.  
  20. in that you must tell DOS which
  21.  
  22. record, and where in the record you
  23.  
  24. want to write.
  25.  
  26.   You pass that information to DOS
  27.  
  28. via channel 15, the Command/Error
  29.  
  30. channel.  All DOS commands and error
  31.  
  32. messages pass through channel 15.
  33.  
  34. To write to a REL file, we have to use
  35.  
  36. two commands -- a POSITION command
  37.  
  38. followed by a PRINT# statement.
  39.  
  40.   POSITION command tells DOS what
  41.  
  42. record to write and where in the
  43.  
  44. record to start writing.  The syntax
  45.  
  46. for the POSITION command is this:
  47.  
  48.  
  49. PRINT#15,"P"CHR$(<channel number>)
  50.             CHR$(<lo record byte>)
  51.             CHR$(<hi record byte>)
  52.             CHR$(<byte in record>)
  53.  
  54.  
  55.   Don't be intimidated by all that
  56.  
  57. gibberish.  It's not hard at all once
  58.  
  59. you see a few examples.
  60.  
  61.  
  62.   File records start with record 1.
  63.  
  64. In each record, the bytes start with
  65.  
  66. number 1. So, to write our PHONEFILE's
  67.  
  68. first record we do this:
  69.  
  70.  
  71. 1000 PRINT#15,"P"CHR$(4)CHR$(1)CHR$(0)
  72.      CHR$(1)
  73. 2000 PRINT#3,"SCHLABOTNIK"
  74. 3000 PRINT#15,"P"CHR$(4)CHR$(1)CHR$(0)
  75.      CHR$(13)
  76. 4000 PRINT#3,"8687247"
  77.  
  78.  
  79.   Now let's tear that apart until we
  80.  
  81. understand it.
  82.  
  83.  
  84.   We PRINT#15 because POSITION is a
  85.  
  86. DOS command, and must be sent through
  87.  
  88. the Command/Error channel.  The "P",
  89.  
  90. of course, stands for POSITION.
  91.  
  92.   CHR$(4) tells DOS we want to use
  93.  
  94. disk channel 4.  You remember from
  95.  
  96. last month when we discussed OPEN,
  97.  
  98. that we opened our file as file 3,
  99.  
  100. unit 8, channel 4, "PHONEFILE".
  101.  
  102.   CHR$(1)CHR$(0) is the record number,
  103.  
  104. which must always be represented by
  105.  
  106. two characters.
  107.  
  108.   Here's how you can calculate the
  109.  
  110. values you must use to access any
  111.  
  112. record number.
  113.  
  114.  
  115. 10 INPUT"Record number";R
  116. 20 HB=INT(R/256)
  117. 30 LB=R-(HB*256)
  118.  
  119.  
  120. Then use LB and HB as the characters
  121.  
  122. for your record number:
  123.  
  124.  
  125. 40 PRINT#15,"P"CHR$(4)CHR$(LB)CHR$(HB)
  126.    CHR$(1)
  127.  
  128.  
  129.   The last parameter of our POSITION
  130.  
  131. command, CHR$(1) is used to point the
  132.  
  133. I/O to the first character of the
  134.  
  135. record.  You can use values ranging
  136.  
  137. from 1 to your record length for this
  138.  
  139. parameter.  Your PRINT# will begin
  140.  
  141. at whatever character in your record
  142.  
  143. this BYTE pointer points to.
  144.  
  145.   Don't omit this BYTE parameter.  If
  146.  
  147. it is left off you will access the
  148.  
  149. 13th character in the record instead
  150.  
  151. of the first.  (That's because the
  152.  
  153. carriage return after the POSITION
  154.  
  155. command would be used as the BYTE
  156.  
  157. parameter.)
  158.  
  159.   Once you have POSITIONED, the PRINT#
  160.  
  161. statement that follows writes into the
  162.  
  163. record sequentially from the character
  164.  
  165. pointed to by the BYTE parameter.  You
  166.  
  167. MUST POSITION every time you PRINT#
  168.  
  169. into a relative file.
  170.  
  171.  
  172. ------- continued in Part 25 ---------
  173.